$(function(){
    $.validator.addMethod("checkName",function(val,input,param){
        reg=/^[a-z]+\w+$/i;
        return reg.test(val);
    },"以首字母开头");

    $.validator.addMethod("checkTel",function(val,input,param){
        reg=/^1[34578]\d{9}$/;
        return reg.test(val);
    },"手机号不合法");
  $("form").validate({
      rules:{
            uname:{
                required:true,
                rangelength:[6,16],
                checkName:true,
                remote: "./../server/checkUser.php",
            },
          upwd:{
                required:true,
                rangelength:[6,20],
          },
          upwdcheck:{
                equalTo:"#upwd"
          },
          utel:{
                required:true,
                checkTel:true
          },
      },
      messages:{
          uname:{
              required:"用户名不能为空",
              rangelength:"长度在{0}-{1}之间",
              remote:"该用户名已存在"
          },
          upwd:{
              required:"密码不能为空",
              rangelength:"长度在{0}-{1}之间",
          },
          upwdcheck:{
              equalTo:"密码不一致"
          },
          utel:{
              required:"手机号不能为空",

          },
      },
      submitHandler:function () {
          //准备数据,发送到后台的php,通过ajax
          //ajax 与服务器进行通信的一种技术,
          // 能够实页面无刷新,调高用户友好体验
          $.ajax({
              url:"./../server/register.php",
              type:"post",
              dataType:"json",
              data:$("form").serialize()
          }).then(function(result){
              if(result.status==1){
                  alert(result.msg);
                  window.location.href="login.html";
                  $("input").html="";

              }else{
                  alert(result.msg);
              }
          })
          return false;
      }
  })
})